﻿#region Copyright (C) 2008, 2009 StreamTv

/* 
 *      Copyright (C) 2008, 2009 StreamTv
 *      http://code.google.com/p/mpstreamtv/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#endregion

#region Imports
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
#endregion

namespace StreamTv2
{
    /// <summary>
    /// Class for serializing objects.
    /// 
    /// Code based on: http://www.dotnetjohn.com/articles.aspx?articleid=173
    /// </summary>
    internal static class SerializerFactory
    {
        #region Public methods
        /// <summary>
        /// Method to convert a custom Object to XML string.
        /// </summary>
        /// <param name="pObject">Object that is to be serialized to XML.</param>
        /// <returns>XML string.</returns>
        public static String SerializeObject(Object pObject)
        {
            try
            {
                String XmlizedString = null;
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(pObject.GetType());
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

                xs.Serialize(xmlTextWriter, pObject);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
                return XmlizedString;
            }
            catch (Exception)
            {
                return null;
            }
        } // SerializeObject

        /// <summary>
        /// Method to reconstruct an Object from XML string.
        /// </summary>
        /// <param name="pXmlizedString">String containing serialized data.</param>
        /// <returns>Deserialized object.</returns>
        public static Object DeserializeObject(String pXmlizedString, Type ofObject)
        {
            XmlSerializer xs = new XmlSerializer(ofObject);
            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

            return xs.Deserialize(memoryStream);
        } // DeserializeObject
        #endregion
        #region Private methods
        /// <summary>
        /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
        /// </summary>
        /// <param name="characters">Unicode Byte Array to be converted to String.</param>
        /// <returns>String converted from Unicode Byte Array.</returns>
        private static String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);
            return (constructedString);
        } // UTF8ByteArrayToString

        /// <summary>
        /// Converts the String to UTF8 Byte array and is used in De serialization.
        /// </summary>
        /// <param name="pXmlString">String containing the serialized data.</param>
        /// <returns>Byte array of the serialized data.</returns>
        private static Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        } // StringToUTF8ByteArray
        #endregion
    }
}
